package com.sevencar.activity;


import java.io.UnsupportedEncodingException;

import com.example.lightble.CmdSend;
import com.example.lightble.R;
import com.example.lightble.db.SettingDatabase;

import me.maxwin.view.DevNameDialog;
import me.maxwin.view.DevNameDialog.onOKListener;
import me.maxwin.view.XListView.IXListViewListener;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.Toast;

public class MineFragment extends Fragment implements IXListViewListener ,onOKListener {

	
	
	private CmdSend mCmdSend;
	@Override
	public void onRefresh() {
		
	}

	@Override
	public View onCreateView(LayoutInflater inflater,
			@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		return inflater.inflate(R.layout.fragment_mine, container, false);
	}
	
	@Override
	public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
		
		RelativeLayout re_card_bag = (RelativeLayout) view.findViewById(R.id.re_card_bag);
		re_card_bag.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				startActivity(new Intent(getActivity(),AboutActivity.class));
			}
		});
		RelativeLayout re_sgroup = (RelativeLayout) view.findViewById(R.id.re_sgroup);
		re_sgroup.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
//				startActivity(new Intent(getActivity(),LightGroupsActivity.class));
				startActivity(new Intent(getActivity(),SimpleActivity.class));
			}
		});
		RelativeLayout re_feed = (RelativeLayout) view.findViewById(R.id.re_feed);
		re_feed.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				startActivity(new Intent(getActivity(),FeedBackActivity.class));
			}
		});
		RelativeLayout re_xiangce = (RelativeLayout) view.findViewById(R.id.re_xiangce);
		re_xiangce.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				
				Toast.makeText(getActivity(), "发送同步数据包", Toast.LENGTH_SHORT).show();
				
				HomeFragment.mBLEServiceInstance.sendData(CmdSend.getInstance().getSyncBianArray());
				
			}
		});
		RelativeLayout re_paw = (RelativeLayout) view.findViewById(R.id.re_paw);
		re_paw.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				DevNameDialog dialog=new DevNameDialog(getActivity(),"设置密码",0);
				dialog.setOnOKListener(MineFragment.this);
				dialog.show();
			}
		});
		RelativeLayout re_name = (RelativeLayout) view.findViewById(R.id.re_name);
		re_name.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				DevNameDialog dialog=new DevNameDialog(getActivity(),"设置名称",1);
				dialog.setOnOKListener(MineFragment.this);
				dialog.show();
			}
		});
		
		
		mCmdSend=CmdSend.getInstance();
		
		
		
	}
	
	@Override
	public void onLoadMore() {
		
	}

	@SuppressLint("NewApi")
	@Override
	public void onOk(DevNameDialog v, int id, String str){

			if(str!=null)
			{
				if(!str.isEmpty())
				{
					if(id==0)  
					{
						Log.i("sno","设置名字, 请重新连接.......已发送....");
						sendPackage(mCmdSend.getPassWdCmdArray(str));
					}else if(id==1){//修改名字.
						Log.i("sno","设送...111111111111.");
						
						 SettingDatabase sdb = new SettingDatabase(getActivity(),0);
						 sdb.execSQL("delete  from   table_temp_device ");
						 sdb.close();
						 
						 HomeFragment.lstDevices.clear();
						 
						sendLightNamePackage(str);
					}
				}
			}
			else
			{
			}

	}
	public void sendLightNamePackage(String str) {
		  byte[] bs;
			try 
			{
				bs = str.getBytes("GB2312");
				byte[] test=mCmdSend.getNameCmdArray(bs);
			    sendPackage(test);
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				Log.i("sno","内码转换失败.....");
			}
	}
	private void sendPackage(byte[] bs){
   	    HomeFragment.mBLEServiceInstance.sendData(bs);
	}

}
